//
//  GLFormSection.m
//  KTools
//
//  Created by zhiyong.kuang on 2017/10/24.
//  Copyright © 2017年 zhiyong.kuang. All rights reserved.
//

#import "GLFormSection.h"
#import "GLFormRow.h"

@interface GLFormSection(){
    
}
@property(nonatomic,strong)NSMutableArray* rows;
@end

@implementation GLFormSection

+(instancetype)formSectionWithTag:(NSString*)tag{
    return [[GLFormSection alloc]initWithTag:tag];
}
-(instancetype)initWithTag:(NSString*)tag{
    if (self = [super init]) {
        _tag = tag;
        [self defaultConfig];
    }
    return self;
}

-(void)defaultConfig{
    self.HeaderHeight = 15.0;
    self.footerHeight = 0.0001;
}

-(void)addFormRow:(GLFormRow*)formRow{
    if (!formRow) {
        return;
    }
    [self.rows addObject:formRow];
}

- (void)addFormRowsFromArray:(NSArray<GLFormRow*> *)otherRows{
    if (!otherRows) {
        return;
    }
    [self.rows addObjectsFromArray:otherRows];
}

- (void)insertFormRow:(GLFormRow*)formRow atIndex:(NSUInteger)index{
    if (!formRow) {
        return;
    }
    [self.rows insertObject:formRow atIndex:index];
}

- (void)insertFormRows:(NSArray<GLFormRow*> *)formRows atIndex:(NSUInteger)index{
    if (!formRows) {
        return;
    }
    [self.rows insertObjects:formRows atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(index, formRows.count)]];
}
- (void)insertFormRows:(NSArray<GLFormRow*> *)formRows atIndexes:(NSIndexSet *)indexes{
    if (!formRows) {
        return;
    }
    [self.rows insertObjects:formRows atIndexes:indexes];
}

- (void)removeLastFormRow{
    [self.rows removeLastObject];
}
- (void)removeFormRowAtIndex:(NSUInteger)index{
    [self.rows removeObjectAtIndex:index];
}
- (void)removeAllFormRows{
    [self.rows removeAllObjects];
}
- (void)removeFormRow:(GLFormRow*)formRow{
    if (!formRow) {
        return;
    }
    [self.rows removeObject:formRow];
}
- (void)removeFormRowsInArray:(NSArray<GLFormRow*> *)otherRows{
    if (!otherRows) {
        return;
    }
    [self.rows removeObjectsInArray:otherRows];
}
- (void)removeFormRowsInRange:(NSRange)range{
    [self.rows removeObjectsInRange:range];
}

- (void)replaceFormRowAtIndex:(NSUInteger)index withFormRow:(GLFormRow*)formRow{
    if (!formRow) {
        return;
    }
    [self.rows replaceObjectAtIndex:index withObject:formRow];
}

#pragma mark- api
-(NSArray *)formRowsWithTag:(NSString *)tag{
    NSMutableArray* array = [NSMutableArray arrayWithCapacity:0];
    for (GLFormRow* row in self.rows) {
        if ([row.tag isEqualToString:tag]) {
            [array addObject:row];
        }
    }
    
    return array;
}
-(GLFormRow *)formRowAtIndex:(NSInteger)index{
    if (self.rows.count > index){
        return [self.rows objectAtIndex:index];
    }
    return nil;
}


#pragma mark- setter and getter
-(NSMutableArray*)rows{
    if (!_rows) {
        _rows = [NSMutableArray arrayWithCapacity:0];
    }
    return _rows;
}
@end
